<template>
    <div class="course-edit">
        <el-button icon="el-icon-back" type="primary" size="mini" @click="goBack">返回</el-button>
        <div class="topbar text-center mt10">
            课程基础设置
        </div>
        <el-form ref="baseForm" :model="form" :rules="rules" label-width="80px" size="small" style="width:600px;" :show-message="false">
            <el-form-item label="课程类型" required  prop="game">
                <el-radio-group v-model="form.game" :disabled="courseId ? true : false">
                    <el-radio :label="0">混排课程</el-radio>
                    <el-radio :label="1">小游戏</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="分类" required prop="category">
                <el-select v-model="form.category" placeholder="请选择分类">
                    <el-option label="英语" :value="1"></el-option>
                    <el-option label="健康" :value="2"></el-option>
                    <el-option label="中文" :value="3"></el-option>
                    <el-option label="STEM" :value="4"></el-option>
                    <el-option label="音乐" :value="5"></el-option>
                    <el-option label="艺术" :value="6"></el-option>
                    <el-option label="数学" :value="7"></el-option>
                    <el-option label="育儿" :value="8"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="课程名称" required prop = "name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="小节名称" required prop="partName">
                <el-input v-model="form.partName"></el-input>
            </el-form-item>
            <el-form-item label="课节封面" required>
                <Upload show-type="avatar" accept-type="image" :exist="editCoverUrl" :limit="1" v-on:upload-change="avatarChanged"></Upload>
            </el-form-item>
            <el-form-item label="上课时间" required prop="startTime">
                <el-date-picker value-format="yyyy/MM/dd HH:mm:ss" v-model="form.startTime" type="datetime" placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="是否上架" required prop="online">
                <el-radio-group v-model="form.online">
                    <el-radio :label="1">立即上架</el-radio>
                    <el-radio :label="0">暂不上架，放入仓库</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item v-if="form.game===1" label="URL地址" required prop="gameUrl">
                <el-input v-model="form.gameUrl"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button icon="el-icon-back" size="default" @click="goBack">返回</el-button>
                <el-button v-if="!courseId" type="primary" size="default" @click="onSubmit">保存</el-button>
                <el-button v-if="courseId" type="primary" size="default" @click="onUpdate">保存</el-button>
                <!-- <el-button v-if="form.game===1" type="primary" size="default" @click="onSubmit">保存并新建</el-button> -->
                <el-button v-if="form.game===0" type="primary" size="default" @click="nextStep">下一步</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import Upload from '@/components/Upload'
    import { insertLesson, getLesson, updateLesson } from '@/api'
    export default {
        components: {
            Upload
        },
        data() {
            return {
                courseId: this.$route.params.id,
                editCoverUrl: '',
                form: {
                    name: '',
                    online: 1,
                    category: '',
                    startTime: '',
                    partName: '',
                    game: 0,
                    coverUrl: '',
                    gameUrl: '',
                    content: '',
                    mix: 0
                },
                rules: {
                    
                }
            }
        },
        mounted() {
            if (this.courseId) {
                getLesson({ id: this.courseId }).then(res => {
                    this.form = res.data.result
                    this.form.id = this.courseId
                    this.editCoverUrl = res.data.result.coverUrl
                })
            }
            let editHistory = window.sessionStorage.courseBaseInfo;
            if(editHistory) {
                this.form = JSON.parse(editHistory)
                this.editCoverUrl = this.form.coverUrl
            }
        },
        methods: {
            avatarChanged(url) {
                this.form.coverUrl = url
            },
            goBack() {
                this.$confirm('返回将丢失本次编辑内容，您确定要返回吗？','提示', {
                    confirmButtonText: '确定返回',
                    cancelButtonText: '继续编辑',
                    cancelButtonClass: 'el-button--primary',
                    confirmButtonClass: 'el-button--reset',
                    type: 'warning'
                }).then(() => {
                    this.$router.go(-1);
                }).catch(()=>{})
                
            },
            onSubmit() {
                insertLesson(this.form).then(res => {
                    this.$message.success('保存成功')
                    setTimeout(function() {
                        this.$router.push('/course')
                    }.bind(this), 2000);
                })

            },
            onUpdate() {
                updateLesson(this.form).then(res => {
                    this.$message.success('保存成功')
                    setTimeout(function() {
                        this.$router.push('/course')
                    }.bind(this), 2000);
                })
            },
            nextStep() {
                this.$refs.baseForm.validate((valid) => {
                    if (valid) {
                        if(!this.form.coverUrl){
                            this.$message.error('请上传课程封面')
                            return false
                        }
                        window.sessionStorage.setItem('courseBaseInfo', JSON.stringify(this.form))
                        this.$router.push('/course/edit/detail' + (this.courseId ? ('/' + this.courseId) : ''))
                    }
                })

            }
        }
    }
</script>

<style scoped lang="scss">
    .course-edit /deep/ {
        .topbar {
            background: #f2f2f2;
            line-height: 30px;
        }

        .avatar-uploader-icon {
            width: 250px;
            height: 150px;
            line-height: 150px;
        }

        .avatar {
            width: 250px;
            height: 150px;
        }
    }
</style>